<script setup lang='ts'>
  import { onMounted, reactive } from 'vue';
  import { propApi } from '@/api/controller';
  import { productTypeArr } from '@/pinia/getter';
  import { Toastloading } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import Pagination from "@/components/Pagination/index.vue";
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  const action = reactive({
    timeObj:getPlayEndTime(getDateArray('ymd')),
    total:0,
    list:[] as any[],
  })
  const initialQuery = {
    page: 1,
    limit: 20,
    type:'d',
    goods_type: 0
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleSerach = () => {
  listQuery.page = 1;
  getList();
};
const resetQuery = () => {
  action.timeObj = getPlayEndTime(getDateArray('ymdhis'))
  Object.assign(listQuery, {
    ...initialQuery
  });
  getList()
};
  const goodsTypeFilters = (type:number = -1)=>{
    const { label='其他' } = productTypeArr.find(res=>res.value == type) || { label:'其他' }
    return label
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:start_date = '', endTime:end_date='' } = action.timeObj
    const res = await propApi.getGoodsStatistics({
      start_date,
      end_date,
      ...listQuery
    })
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <xmDatePicker type="daterange" v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <div class="label">商品类型：</div>
          <el-select v-model="listQuery.goods_type" placeholder="请选择商品类型">
            <el-option label="全部类型" :value="0" />
            <el-option
              v-for="item in productTypeArr"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      show-summary
      border
    >
      <el-table-column width="180" prop="ctime" label="日期" />
      <el-table-column min-width="180" prop="goods_name" label="商品名称" />
      <el-table-column min-width="120" prop="goods_type" label="商品类型">
        <template #default="scoped">
          {{ goodsTypeFilters(scoped.row.goods_type) }}
        </template>
      </el-table-column>
      <el-table-column min-width="180" prop="count" label="总购买次数" />
      <el-table-column min-width="180" prop="price_sum" label="总钻石数" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>